<template>
	<view class="registerbg">
		<view class="bgfff" style="width: 100%; height: 90rpx;line-height: 90rpx;">
			<u-tabs :list="tabList" @click="tabsClick" :scrollable='false' :current='currentTab' lineWidth="40"
				lineColor="#2979ff"></u-tabs>
		</view>
		<view class="pad20">
			<u--form labelPosition="left" ref="uForm">
				<view class="formView">
					<u-form-item label="出矿吨数" prop="name" ref="name" label-width="80px">
						{{rs.producedTons}}
					</u-form-item>
				</view>
				<view class="formView">
					<u-form-item label="出矿时间" prop="name" ref="name" label-width="80px">
						{{rs.producedTime}}
					</u-form-item>
				</view>
				<view class="formView">
					<u-form-item label="出矿票据" prop="name" ref="name" label-width="80px">
						<image @click="clickImg(rs.producedBillImage)" v-if="rs.producedBillImage" class="img1"
							:src="ImgUrl+rs.producedBillImage" mode="scaleToFill"></image>
					</u-form-item>
				</view>
				<view class="formView">
					<u-form-item label="煤运客户" prop="clientName" ref="clientName" label-width="80px">
						{{rs.clientName}}
					</u-form-item>
				</view>

				<view class="formView" v-if="currentTab===0">
					<u-form-item label="识别票据" prop="name" ref="name" label-width="80px" :required='!disabled'>
						<view class="imgView" v-if='disabled'>
							<image @click="clickImg(rs.receiptBillImage)" v-if="rs.receiptBillImage" class="fileimg1"
								:src="ImgUrl+rs.receiptBillImage"></image>
						</view>
						<view class="imgView" v-else>
							<image v-if="!receiptBillImage" class="fileimg1" src="../../static/index/add.png"
								@click="chooseImage"></image>
							<image v-else class="fileimg1" :src="receiptBillImage.path"></image>
							<view v-if="receiptBillImage" class="del" @click="delimg">
								<u-icon name="close-circle-fill" size='20' color='red'></u-icon>
							</view>
						</view>
					</u-form-item>
				</view>

				<view class="formView">
					<u-form-item label="验收吨数" prop="receiptTons" ref="receiptTons" label-width="80px"
						:required='!disabled'>
						<view v-if='disabled'>{{rs.receiptTons}}</view>
						<!-- <u--input v-else type='digit' v-model="receiptTons" border="none" placeholder="请输入"></u--input> -->
						<u--input v-else :disabled="currentTab===0?true:false" disabledColor='#fff' type='digit'
							v-model="receiptTons" border="none" :placeholder="currentTab===0 ? '请识别票据' : '请输入'">
						</u--input>
					</u-form-item>
				</view>

				<view class="formView" v-if="currentTab===1">
					<u-form-item label="验收票据" prop="name" ref="name" label-width="80px" :required='!disabled'>
						<view class="imgView" v-if='disabled'>
							<image @click="clickImg(rs.receiptBillImage)" v-if="rs.receiptBillImage" class="fileimg1"
								:src="ImgUrl+rs.receiptBillImage"></image>
						</view>
						<view class="imgView" v-else>
							<image v-if="!receiptBillImage" class="fileimg1" src="../../static/index/add.png"
								@click="chooseFile"></image>
							<image v-else class="fileimg1" :src="receiptBillImage.path"></image>
							<view v-if="receiptBillImage" class="del" @click="delimg">
								<u-icon name="close-circle-fill" size='20' color='red'></u-icon>
							</view>
						</view>
					</u-form-item>
				</view>
			</u--form>
			<view v-if="!disabled" class="modifybts bgMain colorfff bdRadius20 marAuto marT32" @tap.stop="submit1">提交
			</view>
		</view>
	</view>
</template>

<script>
	import {
		toBase64 as convert
	} from "@/common/base64.js"
	import commonApi from '@/api/common.js'
	export default {
		data() {
			return {
				inputStyle: {
					padding: '15rpx 20rpx',
				},
				disabled: true,
				fileList: [],
				id: '',
				rs: {
					producedTons: ''
				},
				receiptTons: '',
				producedBillImage: '',
				receiptBillImage: null,

				tabList: [{
					name: '识别登记',
					value: 0,
				}, {
					name: '录入登记',
					value: 1,
				}, ],

				currentTab: 0,
			}
		},
		onLoad(e) {
			this.id = e.id;
			this.freightSettlementDetails()
		},
		methods: {
			tabsClick(e) {
				this.producedTime = '';
				this.producedTons = '';
				this.producedBillImage = null;
				this.currentTab = e.value;
			},

			freightSettlementDetails() {
				commonApi.freightSettlementDetails(this.id).then((res) => {
					if (res.status == 200) {
						this.rs = res.data;
						this.disabled = this.rs.receiptTons ? true : false
					}
				});
			},
			clickImg(img) {
				let imgs = []
				imgs.push(img);
				this.lookImg(imgs, 0)
			},

			chooseFile() {
				let that = this;
				wx.chooseImage({
					count: 1,
					success(res) {
						that.receiptBillImage = res.tempFiles[0];
					}
				})
			},

			// 选择本地图片
			chooseImage() {
				uni.chooseImage({
					count: 1,
					success: (res) => {
						uni.showLoading({
							title: '正在识别中...'
						})
						// 下面将图片本地路径转base64
						this.receiptBillImage = res.tempFiles[0]
						convert(res.tempFilePaths[0]).then((res) => {
							this.getAccessToken(res)
						})
					}
				})
			},
			// 获取AccessToken
			getAccessToken(path) {
				uni.request({
					url: 'https://aip.baidubce.com/oauth/2.0/token',
					data: {
						grant_type: 'client_credentials',
						client_id: "m9Iy3VricHzjrZ9a9qa2Kvzk", //高精度含位置
						client_secret: "e17GOEo4Qgs73INy0eghYnTv5tbUw3Lr",
					},
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						this.uploadImage(path, res.data.access_token)
					}
				})
			},
			// 通用文字识别（高精度版）
			uploadImage(path, token) {
				uni.request({
					url: "https://aip.baidubce.com/rest/2.0/ocr/v1/accurate", //高精度含位置
					data: {
						image: path,
						access_token: token
					},
					method: 'POST',
					header: {
						'Content-Type': 'application/x-www-form-urlencoded'
					},
					success: (res) => {
						uni.hideLoading()
						let showEr = res.data.words_result
						//高精度含位置接口处理出矿识别
						for (let i = 0; i < showEr.length; i++) {
							if (showEr[i].words.includes("验收")) {
								//验收吨数
								let receiptTons = showEr[i + 1].words;
								// 判断最后一个字是否为"吨"，如果是则去掉
								if (receiptTons && receiptTons.endsWith('吨')) {
									receiptTons = receiptTons.slice(0, -1);
								}
								this.receiptTons = receiptTons;
								console.info(showEr[i].words, this.receiptTons)
							} else {

							}
						}
						if (this.receiptTons == "") {
							this.showMsg('识别失败，请重试')
						}
					},
					fail() {
						uni.hideLoading()
					}
				})
			},


			delimg() {
				this.receiptBillImage = null;
				if (this.currentTab === 0) {
					this.receiptTons = '';
				}
			},
			submit1() {
				if (!this.receiptTons) {
					this.showMsg('请输入验收吨数')
					return
				}
				if (!this.isNonNegativeFloat(this.receiptTons)) {
					this.showMsg('出矿吨数格式不正确')
					return
				}
				if (!this.receiptBillImage) {
					this.showMsg('请上传验收票据')
					return
				}
				uni.showLoading({
					title: '加载中',
				})
				let that = this;
				wx.uploadFile({
					url: that.baseUrl + '/client/freightSettlement/submit1/' + that.id,
					filePath: that.receiptBillImage.path,
					name: 'receiptBillImage',
					formData: {
						receiptTons: that.receiptTons
					},
					header: {
						'Authorization': uni.getStorageSync('token') || '',
					},
					success(uploadFileRes) {
						uni.hideLoading()
						let rs = JSON.parse(uploadFileRes.data)
						if (rs.status == 200) {
							that.showMsg('操作成功')
							that.stopLocation() //停止监听
							setTimeout(e => {
								uni.navigateBack({
									delta: 1
								});
							}, 1500)
						} else {
							that.showMsg(rs.message)
						}
					},
					fail(red) {
						uni.hideLoading()
						console.log(red)
					}
				})
			},
			
			stopLocation() {
				this.$store.commit('setLocationWatch', false);
				this.$store.commit('setLocationAllow', true);
				// console.log(this.$store.state.locationWatch)
				uni.showToast({
					title: '已停止位置监听',
					icon: 'none'
				});
			}
		}
	}
</script>

<style lang="scss" scoped>
	.registerbg {
		width: 100%;
		min-height: calc(100vh - var(--window-bottom));

		.formView {
			padding: 20rpx;
			background: #fff;
			padding: 10rpx 36rpx;
			margin-top: 20rpx;
			border-radius: 20rpx;

			.topline {
				padding-top: 30rpx;
				border-top: 1rpx solid #E3E4E5;
			}

			.img1 {
				width: 120rpx;
				height: 120rpx;
			}

			.imgView {
				position: relative;
				height: 138rpx;
				width: 138rpx;

				.fileimg1 {
					position: absolute;
					top: 0;
					left: 0;
					width: 138rpx;
					height: 138rpx;
				}

				.del {
					position: absolute;
					right: -10rpx;
					top: -10rpx;
				}
			}
		}

		.modifybts {
			text-align: center;
			height: 90rpx;
			line-height: 90rpx;
		}


		.icon-size {
			width: 40rpx;
			height: 40rpx;
		}
	}

	::v-deep .u-input {
		background-color: #fff !important;
	}
</style>
